//
//  giftSendView.m
//  tcpoctest
//
//  Created by gump on 5/31/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "giftSendView.h"
#import "GiftInfoDataInfo.h"
#import "GiftInfoDataSource.h"
#import "ListInfoGiftView.h"

#import "sendGiftSend.h"
#import "sendPersonReadCoin.h"
#import "sendGiftGetStroage.h"

#import "taskDataSource.h"

#import <QuartzCore/QuartzCore.h>

@implementation giftSendView

-(id)init
{
    self = [super init];
    
    if (self != nil) {
        igamestate = VGS_NULL;
        itimecount = 0;
    }
    
    return self;
}

-(void)initUI
{
    UIView *toptapview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WINDOW_WIDTH, 100)];
    UITapGestureRecognizer *singleTouch = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                  action:@selector(viewTap)];
    [self.view addSubview:toptapview];
    [toptapview addGestureRecognizer:singleTouch];
    
    UIView *bottomtapview = [[UIView alloc] initWithFrame:CGRectMake(0, 340, WINDOW_WIDTH, WINDOW_HIGTH - 340)];
    UITapGestureRecognizer *singleTouchbottom = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                  action:@selector(viewTap)];
    [self.view addSubview:bottomtapview];
    [bottomtapview addGestureRecognizer:singleTouchbottom];
    
    UIImageView *bgview = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 300, 240)];
    bgview.image = [UIImage imageNamed:@"gift_dialogbox_bg.png"];
    bgview.userInteractionEnabled = YES;
    [self.view addSubview:bgview];
    
    UIView  *TextTapView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 240)];
    
    UITapGestureRecognizer *singleTouchbg = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                        action:@selector(bgviewTap)];
    [TextTapView addGestureRecognizer:singleTouchbg];
    
    [bgview addSubview:TextTapView];
    
    UIButton    *sendbtn = [[UIButton alloc] initWithFrame:CGRectMake(25, 170, 80, 40)];
    [sendbtn setBackgroundImage:[UIImage imageNamed:@"gift_button_cancel.png"]
                       forState:UIControlStateNormal];
    [sendbtn setBackgroundImage:[UIImage imageNamed:@"gift_button_cancel_on.png"]
                       forState:UIControlStateHighlighted];
    [sendbtn addTarget:self action:@selector(sendclick) forControlEvents:UIControlEventTouchUpInside];
    [sendbtn setTitle:@"赠送" forState:UIControlStateNormal];
    [sendbtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [bgview addSubview:sendbtn];
    
    UIButton    *canclebtn = [[UIButton alloc] initWithFrame:CGRectMake(195, 170, 80, 40)];
    [canclebtn setBackgroundImage:[UIImage imageNamed:@"gift_button_cancel.png"]
                         forState:UIControlStateNormal];
    [canclebtn setBackgroundImage:[UIImage imageNamed:@"gift_button_cancel_on.png"]
                         forState:UIControlStateHighlighted];
    [canclebtn addTarget:self action:@selector(cancleclick) forControlEvents:UIControlEventTouchUpInside];
    [canclebtn setTitle:@"取消" forState:UIControlStateNormal];
    [bgview addSubview:canclebtn];
    
    headimageview = [[UIImageView alloc] initWithFrame:CGRectMake(25, 30, 80, 80)];
    headimageview.image = [UIImage imageNamed:@"gift_default.png"];
    [bgview addSubview:headimageview];
    
    namelabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 300, 20)];
    namelabel.textAlignment = UITextAlignmentCenter;
    namelabel.backgroundColor = [UIColor clearColor];
    namelabel.textColor = [UIColor whiteColor];
    [bgview addSubview:namelabel];
    
    warnlabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 35, WINDOW_WIDTH - 110, 60)];
    warnlabel.textAlignment = UITextAlignmentCenter;
    warnlabel.backgroundColor = [UIColor clearColor];
    warnlabel.textColor = [UIColor whiteColor];
    warnlabel.numberOfLines = 0;
    [bgview addSubview:warnlabel];
    
    UIImageView *inputBgView = [[UIImageView alloc]initWithFrame:CGRectMake(110, 120, 80, 30)];
    inputBgView.image = [UIImage imageNamed:@"cell.png"];
    inputBgView.userInteractionEnabled = YES;
    [bgview addSubview:inputBgView];
    
    inputTF = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 80, 30)];
    inputTF.font = CENTER_CELL_CONTENT_FONT;
    inputTF.textColor=[UIColor blackColor];
    inputTF.borderStyle=UITextBorderStyleNone;
    inputTF.leftViewMode = UITextFieldViewModeAlways;
    inputTF.placeholder = [NSString stringWithFormat:@"%d",isendcount];
    inputTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    inputTF.delegate =self;
    inputTF.keyboardType = UIKeyboardTypeNumberPad;
    [inputBgView addSubview:inputTF];
    
    UIButton *addcountbtn = [[UIButton alloc] initWithFrame:CGRectMake(210, 118, 30, 30)];
    [addcountbtn setTitle:@"+" forState:UIControlStateNormal];
    [addcountbtn addTarget:self action:@selector(addcount) forControlEvents:UIControlEventTouchUpInside];
    
    [addcountbtn setBackgroundColor: [UIColor blueColor]];
    [addcountbtn.layer setMasksToBounds:YES];
    [addcountbtn.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
    [addcountbtn.layer setBorderWidth:1.0];   //边框宽度
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 1, 0, 0, 1 });
    [addcountbtn.layer setBorderColor:colorref];
    
    [bgview addSubview:addcountbtn];
    
    UIButton *subcountbtn = [[UIButton alloc] initWithFrame:CGRectMake(60, 118, 30, 30)];
    [subcountbtn setTitle:@"--" forState:UIControlStateNormal];
    [subcountbtn addTarget:self action:@selector(subcount) forControlEvents:UIControlEventTouchUpInside];
    
    [subcountbtn setBackgroundColor: [UIColor blueColor]];
    [subcountbtn.layer setMasksToBounds:YES];
    [subcountbtn.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
    [subcountbtn.layer setBorderWidth:1.0];   //边框宽度
    [subcountbtn.layer setBorderColor:colorref];
    
    [bgview addSubview:subcountbtn];
}

-(void)viewTap
{
    if ([inputTF isFirstResponder]) {
        [inputTF resignFirstResponder];
        
        isendcount = [inputTF.text intValue];
        
        if (isendcount <= 0) {
            isendcount = 1;
        }
        
        [self refreshText];
    }
    else
    {
        [[ViewManager sharedViewManager] backViewDirect:self];
    }
}

-(void)bgviewTap
{
    if ([inputTF isFirstResponder]) {
        [inputTF resignFirstResponder];
        
        isendcount = [inputTF.text intValue];
        
        if (isendcount <= 0) {
            isendcount = 1;
        }
        
        [self refreshText];
    }
}

-(void)sendclick
{
    if ([DataControl shareControl].currentFI != nil &&
        self.ginfo != nil) {
        
        float fcoin = [[DataControl shareControl].personDS.uinfo.strcoin floatValue];
        
        if (fcoin >= isendcount * self.ginfo.giftprice) {
            [[netManager sharedNetManager].sendgiftS SendGiftSendGift:self.ginfo.strgiftid
                                                             withMail:
             [DataControl shareControl].giftInfoDS.strSenderMail
                                                            withCount:isendcount];
            
            [[ViewManager sharedViewManager] showProgressHUDWithStatus:@"正在赠送中..."
                                                              withView:self.view];
            
            igamestate = VGS_START;
            itimecount = 0;
        }
        else
        {
            [[ViewManager sharedViewManager] showFailHUDWithStatus:@"金币不够了耶"
                                                         withDelay:2
                                                          withView:self.view];
        }
    
    }
    else
    {
        [[ViewManager sharedViewManager] showFailHUDWithStatus:@"赠送失败了耶"
                                                     withDelay:2
                                                      withView:self.view];
    }
}

-(void)cancleclick
{
    [[ViewManager sharedViewManager] backViewDirect:self];
}

-(void)viewWillShow
{
    if (self.ginfo != nil) {
        if (self.ginfo.giftIsMusic == 2) {
            NSString *musicpath = [[DataControl shareControl].giftInfoDS
                                   GetMusicPathByID:self.ginfo.strgiftid];
            if (musicpath != nil && ![musicpath isEqualToString:@""]) {
                
                NSData *musicdata = [NSData dataWithContentsOfFile:musicpath];
                
                player=[[AVAudioPlayer alloc] initWithData:musicdata  error:nil];
                [player prepareToPlay];
                [player play];
            }
        }
    }
    
    isendcount = 1;
    
    [self refreshView];
}

-(void)refreshView
{
    if (self.ginfo != nil) {
        NSString *imagepath = [[DataControl shareControl].giftInfoDS
                               searchGiftImagePath:self.ginfo.strgiftid];
        
        if (imagepath != nil && ![imagepath isEqualToString:@""]) {
            headimageview.image = [UIImage imageWithContentsOfFile:imagepath];
        }
        
        namelabel.text = self.ginfo.strgiftname;
        
        [self refreshText];
    }
}

-(void)refreshText
{
    int iprice = isendcount * self.ginfo.giftprice / 10000;
    
    if (iprice > 0) {
        warnlabel.text = [NSString stringWithFormat:@"赠送此礼物您需花费\n%d万金币耶",
                          iprice];
    }
    else
    {
        warnlabel.text = [NSString stringWithFormat:@"赠送此礼物您需花费\n%d金币耶",
                          isendcount * self.ginfo.giftprice];
    }
    
    
    inputTF.text = [NSString stringWithFormat:@"%d",isendcount];
}

-(void)addcount
{
    isendcount++;
    
    [self refreshText];
}

-(void)subcount
{
    isendcount--;
    
    if (isendcount <= 0) {
        isendcount = 1;
    }
    
    [self refreshText];
}

-(void)UICustomTick
{
    switch (igamestate) {
        case VGS_START:
        {
            if (itimecount < 200) {
                itimecount++;
            }
            else
            {
                itimecount = 0;
                igamestate = VGS_NULL;
                
                [[ViewManager sharedViewManager] dismissProgressHUD];
            }
        }
            break;
            
        default:
            break;
    }
}

-(void)SendBack
{
    itimecount = 0;
    igamestate = VGS_NULL;
    
    switch ([[DataControl shareControl].giftInfoDS getSendBackState]) {
        case GSBS_SUCCESS:
        {
            if ([DataControl shareControl].currentFI != nil) {
                [[netManager sharedNetManager].sendgiftStroage
                 sendGiftGetTheStroage:[DataControl shareControl].currentFI.mail];
            }
            
            [[ViewManager sharedViewManager] ChangeHUDShowSuccess:@"赠送成功了耶"];
            [self performSelector:@selector(viewTapPop) withObject:nil afterDelay:1.0];
            
            [[DataControl shareControl].taskDS CompleteTaskByID:TI_F_GIFT
             withIsDirectShow:true];
        }
            break;
            case GSBS_LESSCOIN:
        {
            [[ViewManager sharedViewManager] ChangeHUDShowFail:@"金币不够了耶"];
        }
            break;
        case GSBS_DOWN:
        {
            [[ViewManager sharedViewManager] ChangeHUDShowFail:@"此礼物已经下架"];
        }
            break;
        default:
        {
            [[ViewManager sharedViewManager] dismissProgressHUD];
        }
            break;
    }
    
    [[netManager sharedNetManager].sendPersonRC sendReadCoin];

}

-(void)viewTapPop
{
    [[ViewManager sharedViewManager] backViewDirect:self];
    [[ViewManager sharedViewManager].giftListInfoVC PopDialog];
}


#pragma mark -textfield delegate-
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    isendcount = [textField.text intValue];
    
    if (isendcount <= 0) {
        isendcount = 1;
    }
    
    [self refreshText];
    
    return true;
}

@end
